<template>
  <div class="patient-page">
    <nav-bar :title="isSelect ? '选择患者' : '家庭档案'"></nav-bar>
    <div v-if="isSelect" class="patient-select">
      <h3>请选择患者信息</h3>
      <p>以便医生给出更准确的治疗，信息仅医生可见</p>
    </div>
    <div class="patient-list">
      <!-- 患者列表 -->
      <div
        class="patient-item"
        :class="{ selected: patientId === item.id }"
        v-for="item in patientList"
        @click="patientId = item.id"
      >
        <div class="info">
          <span class="name">{{ item.name }}</span
          ><span class="id">{{ item.idCard }}</span
          ><span>{{ item.gender === 1 ? '男' : '女' }}</span
          ><span>{{ item.age }}岁</span>
        </div>
        <div class="icon" @click="editPatient(item)">
          <icon name="user-edit" />
        </div>
        <div class="tag" v-if="item.defaultFlag">默认</div>
      </div>
      <div class="patient-add" @click="addPatient">
        <icon name="user-add" class="cp-icon" />
        <p>添加患者</p>
      </div>
      <div class="patient-tip">最多可添加 6 人</div>
    </div>
    <div v-if="isSelect" class="patient-next">
      <van-button
        :disabled="patientId === ''"
        round
        type="primary"
        block
        @click="next"
        >下一步</van-button
      >
    </div>
    <!-- 右侧弹出的popup -->
    <van-popup v-model:show="show" position="right" width="100vw">
      <nav-bar
        :title="mode === 'add' ? '添加患者' : '编辑患者'"
        :back="hidePopup"
        rightText="保存"
        :rightClick="submit"
      ></nav-bar>
      <van-form>
        <van-field
          v-model="patient.name"
          label="真实姓名"
          placeholder="请输入真实姓名"
        />
        <van-field
          v-model="patient.idCard"
          label="身份证号"
          placeholder="请输入身份证号"
        />
        <van-field label="性别">
          <template #input>
            <radio-button
              :options="options"
              v-model="patient.gender"
            /> </template
        ></van-field>
        <van-field label="默认就诊人">
          <template #input>
            <van-checkbox
              v-model="patient.defaultFlag"
            ></van-checkbox> </template
        ></van-field>
      </van-form>
      <!-- 删除（编辑的时候） -->
      <van-action-bar v-if="mode === 'edit'">
        <van-action-bar-button text="删除" @click="delPatient" />
      </van-action-bar>
    </van-popup>
  </div>
</template>

<script lang="ts" setup name="PatientList">
import { Dialog } from 'vant'
import { ApiRes } from '@/types/data'
import { Patient } from '@/types/patient'
import request from '@/utils/request'
import { Toast } from 'vant'
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import useStore from '@/store'
const { consult } = useStore()
const isSelect = useRoute().query.isSelect
const router = useRouter()
const show = ref(false)
const mode = ref('add' || 'edit')
const options = [
  {
    label: '男',
    value: 1
  },
  {
    label: '女',
    value: 0
  }
]
const patient = ref({} as Patient)
const patientList = ref([] as Patient[])
const getPatientList = async () => {
  const res = await request.get<ApiRes<Patient[]>>('patient/mylist')

  patientList.value = res.data.data
}
onMounted(() => {
  getPatientList()
})
// 选中患者相关
const patientId = ref('')
const next = () => {
  // 保存患者id到仓库
  consult.setPatientId(patientId.value)
  // 跳转到支付页面
  router.push('/consult/pay')
}
// 添加患者
const addPatient = () => {
  show.value = true
  mode.value = 'add'
  patient.value = { gender: 1 } as Patient
}
// 修改患者
const editPatient = (item: Patient) => {
  show.value = true
  mode.value = 'edit'
  patient.value = { ...item }
}
// 隐藏Popup
const hidePopup = () => {
  show.value = false
}
// 提交
const submit = async () => {
  // 判断
  if (!patient.value.name) return Toast('真实姓名不能为空')
  if (!patient.value.idCard) return Toast('身份证号不能为空')
  var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  if (!reg.test(patient.value.idCard)) return Toast('身份证号不正确')

  let res = null
  if (mode.value === 'add') {
    // 新增
    res = await request.post('patient/add', {
      ...patient.value,
      defaultFlag: patient.value.defaultFlag ? 1 : 0
    })
  } else {
    // 修改
    delete patient.value.genderValue
    delete patient.value.age
    res = await request.put('patient/update', {
      ...patient.value,
      defaultFlag: patient.value.defaultFlag ? 1 : 0
    })
  }
  if (res.data.code === 10000) {
    Toast({
      type: 'success',
      message: mode.value === 'add' ? '添加成功' : '编辑成功',
      duration: 1000
    })

    show.value = false
    getPatientList()
  } else {
    Toast({
      type: 'fail',
      message: res.data.message,
      duration: 1000
    })
  }
}
// 删除患者
const delPatient = () => {
  Dialog.confirm({
    title: '提示',
    message: '确定删除该患者吗?'
  })
    .then(async () => {
      await request.delete(`patient/del/${patient.value.id}`)
      Toast({
        message: '删除成功',
        duration: 1000
      })
      show.value = false
      getPatientList()
    })
    .catch(() => {
      // on cancel
    })
}
</script>

<style lang="scss" scoped>
.patient-select {
  padding: 15px;
  > h3 {
    font-weight: normal;
    margin-bottom: 5px;
  }
  > p {
    color: var(--cp-text3);
  }
}
.patient-next {
  padding: 15px;
  background-color: #fff;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 80px;
  box-sizing: border-box;
}
// 底部操作栏
.van-action-bar {
  padding: 0 10px;
  margin-bottom: 10px;
  .van-button {
    color: var(--cp-price);
    background-color: var(--cp-bg);
  }
}
.patient-page {
  padding: 46px 0 80px;
  ::v-deep() {
    .van-popup {
      width: 100%;
      height: 100%;
      padding-top: 46px;
      box-sizing: border-box;
    }
    .van-checkbox__icon {
      font-size: 18px;
    }
  }
}
.patient-list {
  padding: 15px;
}
.patient-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-bottom: 15px;
  position: relative;
  border: 1px solid var(--cp-bg);
  transition: all 0.3s;
  overflow: hidden;
  .info {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    span {
      color: var(--cp-tip);
      margin-right: 20px;
      line-height: 30px;
      &.name {
        font-size: 16px;
        color: var(--cp-text1);
        width: 80px;
        margin-right: 0;
      }
      &.id {
        color: var(--cp-text2);
        width: 180px;
      }
    }
  }
  .icon {
    color: var(--cp-tag);
    width: 20px;
    text-align: center;
  }
  .tag {
    position: absolute;
    right: 60px;
    top: 21px;
    width: 30px;
    height: 16px;
    font-size: 10px;
    color: #fff;
    background-color: var(--cp-primary);
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  &.selected {
    border-color: var(--cp-primary);
    background-color: var(--cp-plain);
    .icon {
      color: var(--cp-primary);
    }
  }
}
.patient-add {
  background-color: var(--cp-bg);
  color: var(--cp-primary);
  text-align: center;
  padding: 15px 0;
  border-radius: 8px;
  .cp-icon {
    font-size: 24px;
  }
}
.patient-tip {
  color: var(--cp-tag);
  padding: 12px 0;
}
</style>
